package swordoffer.offer34;

import java.util.ArrayList;
import java.util.List;

public class BackTrace {
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        dfs(root, target, path, res);
        return res;
    }

    public void dfs(TreeNode root, int target, List<Integer> path, List<List<Integer>> res) {
        if (root == null) {
            return;
        }
        path.add(root.val);
        if (root.left == null && root.right == null && root.val - target == 0) {
            res.add(new ArrayList<>(path));
            path.remove(path.size() - 1);
            return;
        }
        dfs(root.left, target - root.val, path, res);
        dfs(root.right, target - root.val, path, res);
        path.remove(path.size() - 1);
    }
}
